<?php
/**
 * 文章资讯
 */
namespace Wap\Controller;
use Common\Model\CollectModel;
use Common\Model\CommentModel;
use Common\Model\EventsModel;
use Common\Model\FollowModel;
use Common\Model\NoticeModel;
use Common\Model\PointMallModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Wap\BaseController;
use Common\Model\ArticleModel;
use Common\Model\PraiseModel;
class ArticleController extends BaseController {


    public function index(){
        header("Location:".U("Index/index"));
    }

    //文章详情
    public function detail(){
        $articleID=I('get.id',0,'int');
        if(!$articleID){
            $this->error("请指定文章");
        }
        $uid=session("uid")+0;
        $articleInfo = ArticleModel::getArticleDetail($articleID,$uid);
        if($articleInfo['status']!==0){
            $this->error($articleInfo['msg']);
        }
        //点赞
        $praiseInfo = PraiseModel::getPraiseState(OBJ_TYPE_ARTICLE,$articleID,$uid);
        $praiseList = PraiseModel::getPraiseList(OBJ_TYPE_ARTICLE,$articleID,1,5);

        //收藏
        $collect=CollectModel::getCollectState($articleID,$uid);

        //关联
        if($articleInfo['data']['related']){
            $related = explode('|',$articleInfo['data']['related']);
            if($related){
                foreach($related as $val){
                    $relatedInfo[] = explode(',',$val);
                }
                $this->smarty->assign('related',$related);
                foreach($relatedInfo as $k=>$v){
                    if($v[0] == 2){
                        $relatedInfo[$k][] = EventsModel::getArticleAboutEventInfo($v[1]);
                    } else if($v[0] ==4){
                        $relatedInfo[$k][] = PointMallModel::getDetail($v[1],'article_detail');
                    }
                }
                $this->smarty->assign('relatedInfo',$relatedInfo);
            }
        }
        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $articleInfo['data']['title']);
            $this->smarty->assign('wxDesc', $articleInfo['data']['shareDesc']);
            $this->smarty->assign('wxLink', $domain.U('detail?id=' . $articleID . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$articleInfo['data']['listPic']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_ARTICLE);
            $this->smarty->assign('wxObjID',$articleID);
        }


        //是否当日首次签到
        signAssignWap($uid,$this->smarty);
        if($articleInfo['data']['uid']==0){
            $articleInfo['data']['nickname']=SELF_NAME;
        }

        $this->smarty->assign('title',C('SITE_NAME'));
        $this->smarty->assign('obj',$articleInfo['data']);
        //$this->smarty->assign("commentList",$comment['list']);
        $this->smarty->assign("praiseList",$praiseList['list']);

        $this->smarty->assign("hasPraise",$praiseInfo['done']);
        $this->smarty->assign("collected",$collect['done']);
        $this->smarty->display("Article/detail.html");
    }

    //加载更多文章
    public function articleMore(){
        $uid = session('uid') + 0;
        $p   = I('post.p',1,'int');
        $c   = I('post.c',0,'int');
        $articleList= ArticleModel::getArticleList($c,$uid,$p,9,CLIENT_SHOW_WAP);
        $this->smarty->assign('uid',session('uid'));

        $this->smarty->assign('list',$articleList['data']);

        $html = $this->smarty->fetch('Article/article_list_item.html');

        $this->ajaxReturn(array('status'=>0,'dataHTML'=>$html,'isPage'=>$articleList['nextPage']));

    }

    //我的文章加载更多
    public function myArticleMore(){
        $uid = session('uid')+0;

        $page = I('post.p',2,'int');
        $myArticleList = ArticleModel::getMyArticleList($uid,$page,8);
        $this->smarty->assign('list',$myArticleList['list']);
        $html = $this->smarty->fetch('User/articleMore.html');
        $this->ajaxReturn(array('status'=>0,'dataHTML'=>$html,'isPage'=>$myArticleList['nextPage']));
    }

    public function praiseArticle(){
        $id=I('get.id',0,'int');
        if($id<1) $this->ajaxReturn(array('status'=>1));
        if(session('uid')+0<1) $this->ajaxReturn(array('status'=>99));

        $return=PraiseModel::praise(OBJ_TYPE_ARTICLE,$id ,session('uid') ,session('nickname'));
        $this->ajaxReturn($return );
    }

    //文章收藏
    public function collectArticle(){
        $aid = I('get.id',0,'int');
        if($aid<1) $this->ajaxReturn(array('status'=>1));
        if(session('uid')+0<1) $this->ajaxReturn(array('status'=>99));
        $uid = session('uid');

        $data = CollectModel::collectOP($aid,$uid);
        if($data['status']==0){
            $this->ajaxReturn(array('status'=>0));
        }
        else
            $this->ajaxReturn(array('status'=>1));
    }



}